package cn.tedu.springmvc2.response;
import lombok.AllArgsConstructor;
import lombok.Data;

@Data
public class JsonResult {
    /**
     * 响应的状态码
     */
    private Integer code;
    /**
     * 响应的信息
     */
    private String msg;
    /**
     * 响应的数据(一般都是查询的结果可以是单个/多个)
     */
    private Object data;

    //下面是全参构造
    public JsonResult(Integer code,String msg){//含有两个参数的构造方法
        this.code = code;
        this.msg = msg;
    }

    public JsonResult(StatusCode statusCode){
        this.code =  statusCode.getCode();
        this.msg  = statusCode.getMsg();
    }

    public JsonResult(StatusCode statusCode,Object data){
        this.code =  statusCode.getCode();
        this.msg  = statusCode.getMsg();
        this.data = data;
    }
    /**
     *   SUCCESS状态下,直接内置好要返回的成功的码和消息
     *   1. 会返回 成功的码和消息
     *   2. 会返回 成功的码和消息  还有 结果
     */
    private JsonResult(Object data){//带有参数的构造函数
        this.code = StatusCode.SUCCESS.getCode();//成功的码
        this.msg = StatusCode.SUCCESS.getMsg();//成功的消息
        this.data = data;
    }
    public  static JsonResult ok(Object data){//静态有参方法
        return  new JsonResult(data);
    }

    private JsonResult(){//无参构造方法
        this.code = StatusCode.SUCCESS.getCode();//成功的码
        this.msg = StatusCode.SUCCESS.getMsg();//成功的消息
    }
    public  static JsonResult ok(){//静态无参数的重载
        return new JsonResult();
    }
}
